/**
* Oxide
*
* Copyright (c) 2011 Dinosaur Kick Pty Ltd, All rights reserved.
*
* This source code is made available under the "Modified BSD" licensing arrangement.
* See license.txt for details.
*/
// ----------------------------------------------------------------------------

bool dispatch_primaries(void)
{
	char_type ch = *m_cursor;

	if (ch == '\'' || ch == '"')
	{
		return lex_string();
	}
	else if (is_identifier_char(ch, true))
	{
		return lex_identifier();
	}
	else if (std::isdigit(ch))
	{
		return dispatch_numeric();
	}
	else if (m_cursor.advance_if('/'))
	{
		switch (*m_cursor)
		{
			case '/':
				return lex_line_comment();
			case '*':
				return lex_block_comment();
			default:
				return lex_primitive('/');
		}
	}
	
	return lex_sequence('+', '+', TOK_INC)
		|| lex_sequence('-', '-', TOK_DEC)
		|| lex_sequence('<', '<', TOK_SHL, '=', TOK_LTE)
		|| lex_sequence('>', '>', TOK_SHR, '=', TOK_GTE)
		|| lex_primitive();
}

// ----------------------------------------------------------------------------

bool lex_primitive(void)
{
	return m_cursor
		&& lex_primitive(m_cursor.get());
}

bool lex_primitive(char_type ch)
{
	setup_token(static_cast<token_identity>(ch), ch);
	return true;
}

// ----------------------------------------------------------------------------

bool lex_sequence(char_type prev, char_type ch, token_identity identity)
{
	if (m_cursor.advance_if(prev)) {
		if (m_cursor.advance_if(ch))
		{
			setup_token(identity);
			return true;
		}
		return lex_primitive(prev);
	}
	return false;
}

bool lex_sequence(char_type prev, char_type ch1, token_identity identity1, char_type ch2, token_identity identity2)
{
	if (m_cursor.advance_if(prev)) {
		if (m_cursor.advance_if(ch1))
		{
			setup_token(identity1);
			return true;
		}
		else if (m_cursor.advance_if(ch2))
		{
			setup_token(identity2);
			return true;
		}
		return lex_primitive(prev);
	}
	return false;
}

// ----------------------------------------------------------------------------
